package net.skyaims.lori.context;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by wujianing on 14-5-18.
 */
public class FileUtils {

    public static byte[] readToBytes(File file) throws IOException {
        FileInputStream stream = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            System.exit(1);
        }
        byte[] data = new byte[((int) length)];
        byte[] buff = new byte[64];
        int mark = 0;
        int index = 0;
        while (true) {
            mark = stream.read(buff, 0, buff.length);
            if (mark > 0) {
                if (mark == buff.length) {
                    System.arraycopy(buff, 0, data, index, buff.length);
                    index += mark;
                } else {
                    System.arraycopy(buff, 0, data, index, mark);
                    index += mark;
                }
            } else if (mark == -1) {
                break;
            }
        }
        return data;
    }

}
